{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from random import choice \n",
    "from numpy import array, dot, random \n",
    "unit_step = lambda x: 0 if x < 0 else 1 \n",
    "training_data = [ (array([0,0,1]), 0), \n",
    "                 (array([0,1,1]), 1), \n",
    "                 (array([1,0,1]), 1), \n",
    "                 (array([1,1,1]), 1), ] \n",
    "w = random.rand(3) \n",
    "errors = [] \n",
    "eta = 0.2 \n",
    "n = 100 \n",
    "\n",
    "for i in range(n): \n",
    "    x, expected = choice(training_data) \n",
    "    result = dot(w, x) \n",
    "    error = expected - unit_step(result) \n",
    "    errors.append(error) \n",
    "    w += eta * error * x \n",
    "    \n",
    "for x, _ in training_data: \n",
    "    result = dot(x, w) \n",
    "    print(\"{}: {} -> {}\".format(x[:2], result, unit_step(result)))\n",
    "    \n",
    "#source: https://blog.dbrgn.ch/2013/3/26/perceptrons-in-python/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
